//
//  EarthQuakeCharts.swift
//  coredataTest
//
//  Created by xuNick on 16/11/10.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit
import Charts

class EarthQuakeCharts: UIViewController, ChartViewDelegate, IAxisValueFormatter {
    
    @IBOutlet weak var chartView: BarChartView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        chartView.delegate = self
        chartView.chartDescription?.enabled = false
        
        chartView.drawGridBackgroundEnabled = false
        
        chartView.dragEnabled = true
        chartView.setScaleEnabled(true)
        chartView.pinchZoomEnabled = false
        
        // ChartYAxis *leftAxis = chartView.leftAxis;
        
        let xAxis: XAxis = chartView.xAxis
        xAxis.labelPosition = .bottom
        
        chartView.rightAxis.enabled = false
     
        chartView.delegate = self
        
        chartView.drawBarShadowEnabled = false
        chartView.drawValueAboveBarEnabled = true
        
        chartView.maxVisibleCount = 60
        
        xAxis.labelFont = UIFont.systemFont(ofSize: 10.0)
        xAxis.drawGridLinesEnabled = false
        xAxis.granularity = 1.0 // only intervals of 1 day
        xAxis.labelCount = 7
        
        self.updateChartData(count: 10, range: 10.0)
        
    }
    
    func stringForValue(_ value: Double, axis: AxisBase?) -> String {
        return "Q\(value)"
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func updateChartData(count: Int, range: Double){
        var yVals1:[BarChartDataEntry] = []
        for i in 0..<count {
            let mult = range + 1
            let val = Double(arc4random_uniform(UInt32(mult)) + 20)
            yVals1.append(BarChartDataEntry(x: Double(i), y: val))
        }
        var yVals2:[BarChartDataEntry] = []
        for i in 0..<count {
            let mult = range
            let val = Double(arc4random_uniform(UInt32(mult)) + 10)
            yVals2.append(BarChartDataEntry(x: Double(i), y: val))
        }
        let set1 = getChartViewData(entrys: yVals1, label: "dataset 1")
        let set2 = getChartViewData(entrys: yVals2, label: "dataset 2")
        let data = BarChartData(dataSets: [set1, set2])
        data.setValueFont(UIFont(name: "HelveticaNeue-Light", size: 9.0))
        data.setDrawValues(false)
        data.barWidth = 0.7
        chartView.data = data
    }
    
    func getChartViewData(entrys: [ChartDataEntry], label: String) -> (BarChartDataSet){
        var set1:BarChartDataSet? = nil
        if chartView.data != nil{
            set1 = chartView.data?.dataSets[0] as! BarChartDataSet?
            set1?.values = entrys
            chartView.data?.notifyDataChanged()
            chartView.notifyDataSetChanged()
            return set1!
        }else{
            set1 = BarChartDataSet(values: entrys, label: "DataSet 1")
            set1?.highlightColor = UIColor(colorLiteralRed: 244/255.0, green: 117/255.0, blue: 117/255.0, alpha: 1.0)
            set1?.setColor(UIColor.white)
            return set1!
        }
    }
    
    //MARK: - chartViewDelegate
    func chartValueNothingSelected(_ chartView: ChartViewBase) {
        print("chartValueNothingSelected")
    }
    func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
        print("chartValueSelected")
    }
    
    
    
}

class CubicLineSampleFillFormatter: NSObject, IFillFormatter {
    func getFillLinePosition(dataSet: ILineChartDataSet, dataProvider: LineChartDataProvider) -> CGFloat {
        return -10.0
    }
}




